<?xml version='1.0' standalone='yes'?>
<!DOCTYPE PLUGIN>

{% set pluginDirectory = "/usr/local/emhttp/plugins/tailscale" -%}
{% set configDirectory = "/boot/config/plugins/tailscale" -%}
{% set version = env['PLUGIN_VERSION'] -%}
{% if env['PLUGIN_RELEASE'] == "-preview" -%}
{% set branch = "preview" -%}
{% else -%}
{% set branch = "main" -%}
{% endif -%}

<PLUGIN 
  name="{{ name }}"
  author="{{ author }}"
  version="{{ env['PLUGIN_VERSION'] }}"
  {% if launch is defined %}launch="{{ launch }}"{% endif %}
  pluginURL="https://raw.githubusercontent.com/{{ env['GITHUB_REPOSITORY'] }}/{{ branch }}/plugin/{{ name }}{{ env['PLUGIN_RELEASE'] }}.plg"
  {% if support is defined %}support="{{ support }}"{% endif %}
  {% if min is defined %}min="{{ min }}"{% endif %}
  {% if max is defined %}max="{{ max }}"{% endif %}
  {% if icon is defined %}icon="{{ icon }}"{% endif %}
>

<CHANGES>
<![CDATA[
{{ env['PLUGIN_CHANGELOG'] }}

For older releases, see https://github.com/{{ env['GITHUB_REPOSITORY'] }}/releases
]]>
</CHANGES>

<FILE Name="/boot/config/plugins/{{ name }}/{{ tailscaleVersion }}.tgz">
<URL>https://pkgs.tailscale.com/stable/{{ tailscaleVersion }}.tgz</URL>
<SHA256>{{ tailscaleSHA256 }}</SHA256>
</FILE>

<FILE Name="/boot/config/plugins/{{ name }}/{{ package_name }}-{{ env['PLUGIN_VERSION'] }}-noarch-1.txz">
<URL>https://github.com/{{ env['GITHUB_REPOSITORY'] }}/releases/download/{{ env['PLUGIN_VERSION'] }}/{{ package_name }}-{{ env['PLUGIN_VERSION'] }}-noarch-1.txz</URL>
<SHA256>{{ env['PLUGIN_CHECKSUM'] }}</SHA256>
</FILE>

<!--
The 'install' script.
-->
<FILE Run="/bin/bash">
<INLINE>
<![CDATA[
if [ -d "{{ pluginDirectory }}" ]; then
    rm -rf {{ pluginDirectory }}
fi

upgradepkg --install-new /boot/config/plugins/{{ name }}/{{ package_name }}-{{ env['PLUGIN_VERSION'] }}-noarch-1.txz

mkdir -p {{ pluginDirectory }}/bin
tar xzf {{ configDirectory }}/{{ tailscaleVersion }}.tgz --strip-components 1 -C {{ pluginDirectory }}/bin

echo "state" > {{ configDirectory }}/.gitignore

ln -s {{ pluginDirectory }}/bin/tailscale /usr/local/sbin/tailscale
ln -s {{ pluginDirectory }}/bin/tailscaled /usr/local/sbin/tailscaled

# remove other branches (e.g., if switching from main to preview)
{% if branch != 'main' -%}
rm -f /boot/config/plugins/tailscale.plg
rm -f /var/log/plugins/tailscale.plg
{% endif -%}
{% if branch != 'preview' -%}
rm -f /boot/config/plugins/tailscale-preview.plg
rm -f /var/log/plugins/tailscale-preview.plg
{% endif -%}
{% if branch != 'trunk' -%}
rm -f /boot/config/plugins/tailscale-trunk.plg
rm -f /var/log/plugins/tailscale-trunk.plg
{% endif %}

{% if branch != 'main' -%}
# Update plugin name for non-main branches
sed -i "s/Tailscale\*\*/Tailscale ({{ branch.capitalize() }})**/" {{ pluginDirectory }}/README.md
{% endif %}

# start tailscaled
{{ pluginDirectory }}/restart.sh

# Bash completion
tailscale completion bash > /etc/bash_completion.d/tailscale

# cleanup old versions
rm -f $(ls /boot/config/plugins/{{ name }}/{{ package_name }}-*.txz 2>/dev/null | grep -v '{{ env['PLUGIN_VERSION'] }}')
rm -f $(ls /boot/config/plugins/{{ name }}/*.tgz 2>/dev/null | grep -v '{{ tailscaleVersion }}')

# check to see if the state file has been backed up to Unraid Connect
if [ -d "/boot/.git" ]; then
        if git --git-dir /boot/.git log --all --name-only --diff-filter=A -- config/plugins/tailscale/state/tailscaled.state | grep -q . ; then
            echo "******************************"
            echo "*          WARNING           *"
            echo "******************************"
            echo " "
            echo "The Tailscale state file has been backed up to Unraid Connect via Flash backup." 
            echo " "
            echo "To remove this backup, please perform the following steps:"
            echo "1. Go to Settings -> Management Access".
            echo "2. Under Unraid Connect, deactivate flash backup. Select the option to also delete cloud backup."
            echo "3. Reactivate flash backup."

            /usr/local/emhttp/webGui/scripts/notify -l '/Settings/ManagementAccess' -i 'alert' -e 'Tailscale State' -s 'Tailscale state backed up to Unraid connect.' -d 'The Tailscale state file has been backed up to Unraid connect. This is a potential security risk. From the Management Settings page, deactivate flash backup and delete cloud backups, then reactivate flash backup.'
        fi
fi

echo ""
echo "----------------------------------------------------"
echo " {{ name }} has been installed."
echo " Version: {{ env['PLUGIN_VERSION'] }}"
echo "----------------------------------------------------"
echo ""
]]>
</INLINE>
</FILE>

<!--
The 'remove' script.
-->
<FILE Run="/bin/bash" Method="remove">
<INLINE>
<![CDATA[
# Stop service
/etc/rc.d/rc.tailscale stop 2>/dev/null

rm /usr/local/sbin/tailscale
rm /usr/local/sbin/tailscaled

removepkg unraid-tailscale-utils

rm -rf {{ pluginDirectory }}
rm -f {{ configDirectory }}/*.tgz
rm -f {{ configDirectory }}/*.txz
]]>
</INLINE>
</FILE>

</PLUGIN>
